{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9083369a",
   "metadata": {},
   "source": [
    "# 人脸比对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "98a57145",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679303664,6d8cc9f2-0f94-4d06-b345-b60fdc3a10db', 'time_used': 1267, 'confidence': 87.68, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': 'b849e15afe2f0f83656dfb27baf9e7fe', 'face_rectangle': {'top': 173, 'left': 132, 'width': 183, 'height': 183}}], 'faces2': [{'face_token': '1ea1bafea37739bbfc200f262d819461', 'face_rectangle': {'top': 533, 'left': 321, 'width': 546, 'height': 546}}], 'image_id1': 'gv+3Skfjdikghvr65zRf5A==', 'image_id2': 'f6lKmYXAm5ZkB2HqPGprrg=='}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "face++ 人脸比对\n",
    "'''\n",
    "# 首先修改url\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('ms_01.jpg', 'rb')\n",
    "f_02 = open('ms_02.jpg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "params = {\n",
    "    'api_key':'4kJky2HgIBMQBCY9xyvRQGdTb4kDjFkK',\n",
    "    'api_secret':'Ux9dWCWNBcVVFH44AZ8MwFyXh_U29Za1',\n",
    "    'image_base64_1':img_01,\n",
    "    'image_base64_2':img_02\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f20d650",
   "metadata": {},
   "source": [
    "# 人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb755a96",
   "metadata": {},
   "source": [
    "### 创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9c67c5eb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': '12c11ef6b48667b05afb58cd1760f969', 'time_used': 163, 'face_count': 0, 'face_added': 0, 'request_id': '1679303969,69049958-f280-4569-b4b7-8e4e432751dd', 'outer_id': '', 'failure_detail': []}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "# 首先修改url\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "\n",
    "params = {\n",
    "    'api_key':'4kJky2HgIBMQBCY9xyvRQGdTb4kDjFkK',\n",
    "    'api_secret':'Ux9dWCWNBcVVFH44AZ8MwFyXh_U29Za1',\n",
    "    'display_name':'汤臣一品8栋'\n",
    "}\n",
    "\n",
    "\n",
    "response = requests.post(request_url, data=params,)\n",
    "\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23f5c43a",
   "metadata": {},
   "source": [
    "### 检查人脸数据集情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ec9e95c0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': '12c11ef6b48667b05afb58cd1760f969', 'tags': '', 'time_used': 101, 'user_data': '', 'display_name': '汤臣一品8栋', 'face_tokens': [], 'face_count': 0, 'request_id': '1679304236,5d9e5a08-8ca1-473d-9bcd-8c47209c157f', 'outer_id': ''}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "'''\n",
    "检查人脸数据集\n",
    "'''\n",
    "# 首先修改url\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "\n",
    "params = {\n",
    "    'api_key':'4kJky2HgIBMQBCY9xyvRQGdTb4kDjFkK',\n",
    "    'api_secret':'Ux9dWCWNBcVVFH44AZ8MwFyXh_U29Za1',\n",
    "    'faceset_token':'12c11ef6b48667b05afb58cd1760f969'\n",
    "}\n",
    "\n",
    "\n",
    "response = requests.post(request_url, data=params,)\n",
    "\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6874132",
   "metadata": {},
   "source": [
    "### 添加人脸"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2abb560",
   "metadata": {},
   "source": [
    "#### 先检测人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b050a691",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679305226,1608ea00-719a-463b-b357-6c2d19fd4e9d', 'time_used': 90, 'faces': [{'face_token': '5aabd75089710931f21a2366a810cffb', 'face_rectangle': {'top': 80, 'left': 275, 'width': 113, 'height': 113}}], 'image_id': 'hK1HEqzQnbw3NPfOQOrocw==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸数据添加\n",
    "'''\n",
    "# 首先修改url\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('c.jpg', 'rb')\n",
    "\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = {\n",
    "    'api_key':'4kJky2HgIBMQBCY9xyvRQGdTb4kDjFkK',\n",
    "    'api_secret':'Ux9dWCWNBcVVFH44AZ8MwFyXh_U29Za1',\n",
    "    'image_base64':img,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c61129f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': '12c11ef6b48667b05afb58cd1760f969', 'time_used': 504, 'face_count': 3, 'face_added': 1, 'request_id': '1679305242,fb4b108c-a529-4303-b9af-7a934eb55a86', 'outer_id': '', 'failure_detail': []}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "'''\n",
    "检查人脸数据集\n",
    "'''\n",
    "# 首先修改url\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "\n",
    "params = {\n",
    "    'api_key':'4kJky2HgIBMQBCY9xyvRQGdTb4kDjFkK',\n",
    "    'api_secret':'Ux9dWCWNBcVVFH44AZ8MwFyXh_U29Za1',\n",
    "    'faceset_token':'12c11ef6b48667b05afb58cd1760f969',\n",
    "    'face_tokens':'5aabd75089710931f21a2366a810cffb'\n",
    "}\n",
    "\n",
    "\n",
    "response = requests.post(request_url, data=params,)\n",
    "\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "026dc376",
   "metadata": {},
   "source": [
    "## 人脸搜索（课后）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8a755f9b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'image_id': '+oUKGbyASAf0+4Z2EsHQFA==', 'faces': [{'face_rectangle': {'width': 102, 'top': 86, 'left': 259, 'height': 102}, 'face_token': '1a11a830ea38de395450276810354f9c'}], 'time_used': 482, 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101}, 'request_id': '1679305671,7242a8fc-5307-4473-89f9-0cc99e7f9118', 'results': [{'confidence': 86.514, 'user_id': '', 'face_token': '5aabd75089710931f21a2366a810cffb'}]}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸搜索\n",
    "'''\n",
    "# 首先修改url\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('c2.jpg', 'rb')\n",
    "\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = {\n",
    "    'api_key':'4kJky2HgIBMQBCY9xyvRQGdTb4kDjFkK',\n",
    "    'api_secret':'Ux9dWCWNBcVVFH44AZ8MwFyXh_U29Za1',\n",
    "    'image_base64':img,\n",
    "    'faceset_token':'12c11ef6b48667b05afb58cd1760f969'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bcd8fb42",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
